package com.hanter.downloader;

import androidx.annotation.Nullable;

import com.hanter.downloader.service.ResponseWrapper;
import com.hanter.downloader.slice.PartialMbSlicer;
import com.hanter.downloader.slice.PartialSlicer;

import java.util.ArrayList;
import java.util.List;

/**
 * 下载任务信息，包含文件名 断点支持 状态 校验 分块
 */
public class TaskInfo {


    /**
     * 默认就是下载中，即使进度 0
     */
    public static final int STATUS_STOPPED = -1;
    public static final int STATUS_IN_PROGRESS = 0;
    public static final int STATUS_PAUSED = 1;
    public static final int STATUS_SUCCESSFUL = 2;
    public static final int STATUS_FAILED = 3;

    private static final PartialSlicer slicer = new PartialMbSlicer();

    public static TaskInfo newTaskInfo(ResponseWrapper responseWrapper) {
        // 长度为 0 的情况
        TaskInfo taskInfo = new TaskInfo(responseWrapper.url, null, null);
        long fileSize = toLong(responseWrapper.getHeader("Content-Length"));
        taskInfo.setContentLength(fileSize);
        String acceptRanges = responseWrapper.getHeader("Accept-Ranges");
        taskInfo.setRangeSupported(!acceptRanges.equalsIgnoreCase("none") && fileSize > 0);
        taskInfo.setPartials(slicer.slice(fileSize));
        return taskInfo;
    }

    private static long toLong(@Nullable String num) {
        if (num == null) return -1;
        try {
            return Long.parseLong(num);
        } catch (Exception e) {
            return -1;
        }
    }

    public TaskInfo() {
    }

    public TaskInfo(String url, String name, String path) {
        this.url = url;
        this.name = name;
        this.path = path;
    }

    /**
     * url :
     * name :
     * check : [{"name":"md5","digest":""}]
     * redirectUrl : null
     * rangeSupported : true
     * contentLength : 100
     * status : 0
     * partials : [{"start":0,"end":100,"offset":2}]
     */



    private String url;
    private String name;
    private String path;
    private String redirectUrl;
    private boolean rangeSupported;
    private long contentLength;
    private int status;
    private List<CheckBean> check;
    private List<Partial> partials;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getRedirectUrl() {
        return redirectUrl;
    }

    public void setRedirectUrl(String redirectUrl) {
        this.redirectUrl = redirectUrl;
    }

    public boolean isRangeSupported() {
        return rangeSupported;
    }

    public void setRangeSupported(boolean rangeSupported) {
        this.rangeSupported = rangeSupported;
    }

    public long getContentLength() {
        return contentLength;
    }

    public void setContentLength(long contentLength) {
        this.contentLength = contentLength;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public List<CheckBean> getCheck() {
        return check;
    }

    public void setCheck(List<CheckBean> check) {
        this.check = check;
    }

    public List<Partial> getPartials() {
        return partials;
    }

    public void setPartials(List<Partial> partials) {
        this.partials = partials;
    }

    public static class CheckBean {
        /**
         * name : md5
         * digest :
         */

        private String name;
        private String digest;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDigest() {
            return digest;
        }

        public void setDigest(String digest) {
            this.digest = digest;
        }
    }
}
